﻿/*
 * Created by SharpDevelop.
 * User: tdptcj
 * Date: 10/11/2011
 * Time: 2:54
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Windows;

using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using GalaSoft.MvvmLight.Messaging;
using MetaVideoEditorEvolved.Messages;

namespace MetaVideoEditorEvolved.ViewModel
{
	/// <summary>
	/// Description of MappingGenreWindowVM.
	/// </summary>
	public class MappingGenreWindowVM : ViewModelBase
	{
		private List<MetaVideoEditorEvolved.Engine.GenresMappingGenreMapping> genresMapping;
		private RelayCommand saveCommand;
		private RelayCommand syncGenresCommand;
		
		public RelayCommand SyncGenresCommand
		{
			get
			{
				return syncGenresCommand;
			}

			set
			{
				syncGenresCommand = value;
				this.RaisePropertyChanged("SyncGenresCommand");
			}
		}
		
		public List<string> VideoTypes
		{
			get
			{
				return new List<string>() {"All", "Movie", "Series"};
			}
		}
		
		public RelayCommand SaveCommand
		{
			get
			{
				return saveCommand;
			}

			set
			{
				saveCommand = value;
				this.RaisePropertyChanged("SaveCommand");
			}
		}
		
		public List<MetaVideoEditorEvolved.Engine.GenresMappingGenreMapping> GenresMapping
		{
			get
			{
				return genresMapping;
			}
			
			set
			{
				genresMapping = value;
				RaisePropertyChanged("GenresMapping");
			}
		}
		
		public MappingGenreWindowVM()
		{
			this.genresMapping = MetaVideoEditorEvolved.Engine.GenresMappingManager.mappingTable.GenreMapping.ToList().OrderBy(g=>g.Original).ToList();
			
			this.saveCommand = new RelayCommand(()=>
			                                    {
			                                    	var rule = new ValidationRules.MappingGenreValidation();
			                                    	var hasError = (from mapping in this.GenresMapping
			                                    	                where !rule.Validate(mapping,CultureInfo.InvariantCulture).IsValid
			                                    	                select mapping).Any();
			                                    	
			                                    	if (hasError) {
			                                    		MessageBox.Show("Il y a des erreurs, veuillez corriger","MVEE", MessageBoxButton.OK, MessageBoxImage.Error);
			                                    	}
			                                    	else
			                                    	{
			                                    		MetaVideoEditorEvolved.Engine.GenresMappingManager.mappingTable.GenreMapping = this.GenresMapping.ToArray();
			                                    		MetaVideoEditorEvolved.Engine.GenresMappingManager.SaveMappingTable();
			                                    		
			                                    		Messenger.Default.Send<CloseMessage>(new CloseMessage { WindowToClose = Messages.WindowType.GenreMappingWindow });
			                                    	}
			                                    });
			
			var syncAction = new Action(()=>
			                            {
			                            	if (MessageBox.Show("Cette action va resauvergarder tout vos médias, êtes-vous sûr?","MVEE",MessageBoxButton.YesNo,MessageBoxImage.Question,MessageBoxResult.No)== MessageBoxResult.Yes) {
			                            		var helper = new Helper.KernelHelper();
			                            		
			                            		foreach (var media in UI.KernelSessionManager.SessionInstance.KernelUiEntity.FlatListOfMediaItems) {
			                            			var video = media as UI.Entity.VideoMediaElement;
			                            			if (video!=null) {
			                            				helper.SaveItem(video);
			                            			}
			                            		}
			                            	}
			                            });
			
			this.syncGenresCommand = new RelayCommand(()=>
			                                          {
			                                          	var waiting = new WaitingWindow(syncAction, UI.KernelSessionManager.SessionInstance.RealKernel.GetString("SynchronizingGenres"));
			                                          	waiting.ShowDialog();
			                                          });
		}
	}
}
